//
//  ZsBaseViewController.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/4/22.
//

import UIKit
import IQKeyboardManagerSwift
import SwiftyJSON

class ZsBaseViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        IQKeyboardManager.shared.enableAutoToolbar = false;
    }
    override func viewWillAppear(_ animated: Bool) {
        super .viewWillAppear(animated)
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    func saveUserInfo(Json: JSON) {
//        let dataDic = Json.dictionary
        
        let jsonData = Json.description.data(using: String.Encoding.utf8, allowLossyConversion: false)!
        let dataDic = try? JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as? NSDictionary
                
        let avatarPath = dataDic?.object(forKey: "avatarPath")
        UserDefaults.standard.setValue(avatarPath , forKey:ActionHeader.UserDefault_AvatarPath)

        let phone =  dataDic?.object(forKey: "phone") as! String
        let roleType:Array = dataDic?.object(forKey: "roleType") as! Array<Any>
        
//        for obj in roleType {
//            print(obj)
//        }
        
        let userId = dataDic?.object(forKey: "userId")
        let username = dataDic?.object(forKey: "username")
        let userType = dataDic?.object(forKey: "userType")
        let phoneCode = dataDic?.object(forKey: "phoneCode")
        let companyCode = dataDic?.object(forKey: "companyCode")
        let staffCode = dataDic?.object(forKey: "staffCode")
        
        UserDefaults.standard.setValue(phone , forKey:ActionHeader.UserDefault_Phone)
        UserDefaults.standard.setValue(userType , forKey:ActionHeader.UserDefault_UserType)
        UserDefaults.standard.setValue(roleType , forKey:ActionHeader.UserDefault_RoleType)
        UserDefaults.standard.setValue(userId , forKey:ActionHeader.UserDefault_UserId)
        UserDefaults.standard.setValue(username, forKey:ActionHeader.UserDefault_UserName)
        UserDefaults.standard.setValue(phoneCode, forKey:ActionHeader.UserDefault_PhoneCode)
        UserDefaults.standard.setValue(phone, forKey:ActionHeader.WLloginAccount)
        UserDefaults.standard.setValue(staffCode, forKey:ActionHeader.UserDefault_StaffCode)
        UserDefaults.standard.setValue(companyCode, forKey:ActionHeader.UserDefault_CompanyCode)
 
    }
    
    func getUserInfo() {
        HttpService.sharedHttpService().getUserInfoMsg { (Json, state, error) in
            Constants.window?.hideToastActivity()
            Constants.window?.hideAllToasts()
            if state {
                self.saveUserInfo(Json: Json)
                let optionModel:OptionModel = OptionModel.init(key: ActionHeader.WLAutoLogin)
                optionModel.value = "1";
                CacheTool.cacheTool.addOptiontWith(item: optionModel)
                
                let tabBarController = ZsTabBarController.init()
                Constants.window?.rootViewController = tabBarController
                Constants.window?.makeKeyAndVisible()
            } else {
                Constants.window?.makeToast(error)
            }
        }
    }
    
    func refreshData() {
        
    }
}
